package org.example.j2024_11_26.controller;

import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Date;


@RestController
@RequestMapping("/captcha")
public class Captcha {
    private static final String CAPTCHA_KEY = "ADMIN_KAPTCHA_SESSION_KEY";
    private static final String CAPTCHA_CREATE_DATE = "ADMIN_KAPTCHA_SESSION_DATE";
    private static final long TIME_OUT = 60 * 1000 ; // 毫秒
    @RequestMapping("/check")
    public boolean check(String inputCaptcha , HttpSession session){
        if(!StringUtils.hasLength(inputCaptcha)){
            return false ;
        }
        String key = (String) session.getAttribute(CAPTCHA_KEY);
        Date createDate = (Date) session.getAttribute(CAPTCHA_CREATE_DATE);
        long currentDate = System.currentTimeMillis();
        if(key != null && (key.equals(inputCaptcha) && createDate.getTime()-currentDate<TIME_OUT)){
            return true ;
        }
        return false ;
    }
}
